/**
 * <p>Title: Mob.java</p>
 * <p>Description: CS 343 Final Project. This class contains the mob elements for the game</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package Charactors;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import Components.SoundEffects;
import GameState.Game;
import Obstacles.DropableObstacle;

public class Mob {

	private Image[] sprite;
	private BufferedImage allSprites;
	private int xLoc, yLoc;
	private int spriteLoc;
	private long lastSpriteTime;
	private int spriteTime = 100;
	private DropableObstacle ob;

	/**
	 * @param none
	 * @description gets xLoc of the sprite
	 */
	public int getxLoc() {
		return xLoc;
	}

	/**
	 * @param xLoc
	 * @description sets the xLoc of the sprite
	 */
	public void setxLoc(int xLoc) {
		this.xLoc = xLoc;
	}

	/**
	 * @param none
	 * @description gets yLoc of the sprite
	 */
	public int getyLoc() {
		return yLoc;
	}

	/**
	 * @param yLoc
	 * @description sets the yLoc of the sprite
	 */
	public void setyLoc(int yLoc) {
		this.yLoc = yLoc;
	}

	/**
	 * @param none
	 * @description creating enemy sprites to appear in game
	 */
	public Mob() {
		xLoc = -100;
		yLoc = 100;
		ob = new DropableObstacle();
		SoundEffects a = new SoundEffects();
		a.playEagleSound();
		Game.getObstacleContainer().addObstacle(ob);
		try {
			allSprites = ImageIO.read(new File("src/images/eagle.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		int totalHeight = allSprites.getHeight(null);
		sprite = new Image[8];
		int count = 0;
		for (int i = 0; i < 3; i++) {
			sprite[count] = allSprites.getSubimage(allSprites.getWidth() / 3
					* i, 0, allSprites.getWidth() / 3, totalHeight / 3);
			count++;
		}
		for (int i = 0; i < 3; i++) {
			sprite[count] = allSprites.getSubimage(allSprites.getWidth() / 3
					* i, totalHeight / 3, allSprites.getWidth() / 3,
					totalHeight / 3);
			count++;
		}
		for (int i = 0; i < 2; i++) {
			sprite[count] = allSprites.getSubimage(allSprites.getWidth() / 3
					* i, totalHeight / 3 * 2, allSprites.getWidth() / 3,
					totalHeight / 3);
			count++;
		}

	}

	/**
	 * @param none
	 * @description moves the xLoc of the sprite by 3 each iteration
	 */
	public void move() {
		if (getxLoc() >= Game.player.getxLoc() && isHolding())
			drop();
		xLoc += 3;
	}

	/**
	 * @param none
	 * @description this is in charge of dropping the obstacle from the sky
	 */
	public void drop() {
		ob.drop();
	}

	/**
	 * @param none
	 * @description finds out if the mob sprite is holding an object
	 */
	public boolean isHolding() {
		return !ob.isDropped();
	}

	/**
	 * @param none
	 * @description gets the image of the sprite
	 */
	public Image getImage() {
		if (System.currentTimeMillis() > lastSpriteTime + spriteTime) { // wait
																		// to
																		// show
																		// spite...
			spriteLoc++;
			lastSpriteTime = System.currentTimeMillis();
			spriteLoc = spriteLoc % 8;
		}
		return sprite[spriteLoc];
	}
}
